import axios from "axios";
import qs from 'qs';
console.log(process.env.NODE_ENV);
switch(process.env.NODE_ENV){
    case "production":
        axios.defaults.baseURL = "http://129.168.110.129:8000";
        break;
    case "text":
        axios.defaults.baseURL = "http://129.168.110.129:8000";
        break;
    default:
        axios.defaults.baseURL = 'http://localhost:8000'
}
let instance = axios.create({
    timeout:50000
});
instance.interceptors.request.use((config)=>{
    console.log(config);
    return config;
},err=>{
    console.log(err);
    return err
})
instance.interceptors.response.use(res=>{
    console.log(res);
    return res;
},err=>{
    console.log(err);
    return err;
})
let request = function({method,url,arg,headerType}){
    let data = {};
    if(method.toLowerCase() == 'get'){
        if(headerType == 'form'){
            instance.defaults['Content-Type'] = "application/x-www-form-urlencoded;charset=utf-8";
        }
        if(arg){
            arg = qs.stringify(arg);
            url += "?" +arg;
        }
    }
    if(method.toLowerCase() == 'post'){
        if(headerType == 'form'){
            instance.defaults['Content-Type'] = "application/x-www-form-urlencoded;charset=utf-8";
            data = qs.stringify(arg);
        }else {
            instance.defaults["Content-Type"] = "application/json;charset=utf-8";
            data = arg;
        }
    }
    return new Promise((resolve,reject)=>{
        instance({
            method,
            url,
            data
        }).then(res=>{
            console.log('成功的值'+res);
            resolve(res);
        }).catch(err=>{
            console.log(err);
            reject(err);
        })
    })
}
export default request;